package com.google.code.openperfmon.web.admin;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.builder.CompareToBuilder;

import com.google.code.openperfmon.domain.MonitorDefinition;
import com.google.code.openperfmon.domain.OutputVariable;
import com.google.code.openperfmon.service.ExecutionContext;
import com.google.code.openperfmon.service.MonitorDefinitionService;
import com.google.code.openperfmon.service.NonUniqueFieldException;
import com.google.code.openperfmon.service.ServiceFactory;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AddEditMonitorDefinitionAction extends ActionSupport {
	private Long id;
	private String testHost;
	private MonitorDefinition definition;
	private ExecutionContext testCtx;


	@Override
	public String input() throws Exception {
		if (id == null) {
			definition = new MonitorDefinition();
			definition.setBody("ctx.setStatus(Status.Green);\nctx.setMessage('Hello World');\n");
		}
		else {
			definition = ServiceFactory.getMonitorDefinitionService().getById(id);
		}
		testHost = MonitorDefinitionService.DEFAULT_TEST_HOST;
		testCtx = null;
		return super.input();
	}
	
	public String save() throws Exception {
		testCtx = ServiceFactory.getExecutionService().test(definition, testHost);
		if (! testCtx.isSuccess()) return INPUT; 
		try {
			ServiceFactory.getMonitorDefinitionService().saveMonitorDefinition(definition);
		} 
		catch (NonUniqueFieldException e) {
			addActionError(e.getMessage());
			testHost = MonitorDefinitionService.DEFAULT_TEST_HOST;
			return INPUT;
		}
		return SUCCESS;

	}
	
	public String test() throws Exception {
		testCtx = ServiceFactory.getExecutionService().test(definition, testHost);
		return SUCCESS;
	}
	
	public MonitorDefinition getDefinition() {
		return definition;
	}
	public void setDefinition(MonitorDefinition definition) {
		this.definition = definition;
	}

	public String getTestHost() {
		return testHost;
	}

	public void setTestHost(String testHost) {
		this.testHost = testHost;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public boolean isTestComplete() {
		return testCtx != null;
	}
	
	public boolean isHavingOutput() {
		return testCtx.getOutput() != null && testCtx.getOutput().size() > 0; 
	}
	
	public List<OutputVariable> getTestOutput() {
		List<OutputVariable> out = testCtx.getOutput();
		Collections.sort(out, new Comparator<OutputVariable>() {
			public int compare(OutputVariable o1, OutputVariable o2) {
				return new CompareToBuilder()
					.append(o1.getVarName(), o2.getVarName())
					.append(o1.getKey(), o2.getKey())
					.toComparison();
			}
		});
		return out;
	}
	
	public String getTestCss() {
		return testCtx.isSuccess() ? "testSuccess" : "testError";
	}

	public ExecutionContext getTestCtx() {
		return testCtx;
	}
}
